
package com.jymf.util;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;


/**
 * JSON处理类
 * 
 * @author Jiangyu
 * 
 */
public class HttpJsonObject {

	public Integer page = 1;// 默认
	public Integer pageSize = 10;
	private static JsonConfig config = new JsonConfig();
	Log log = LogFactory.getLog(HttpJsonObject.class);
	static{
    	config.registerJsonValueProcessor(java.sql.Date.class, new DateJsonValueProcessor());
	}
	
	/**
	 * 把单个对象的属性与值按json的数据格式输出
	 * 
	 * @param obj
	 *            pojo对象
	 */

	public HttpServletRequest getRequest() {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes()).getRequest();
		return request;
	}


	public void outJsonHtml(Object obj,HttpServletResponse response) {
		outJsonHtmlString(JSONObject.fromObject(obj).toString(),response);
	}

	public void outJson(Object obj,HttpServletResponse response) {
		config.registerJsonValueProcessor(Date.class, new DateJsonValueProcessor());
		outJsonString(JSONObject.fromObject(obj,config).toString(),response);
	}

	/**
	 * 把多个对象的集合按照json数据格式打印
	 * 
	 * @param array
	 */
	public void outJsonArray(Object array,HttpServletResponse response) {
		outJsonString(JSONArray.fromObject(array).toString(),response);
	}

	/**
	 * 输出对象数组的json数据,如[{"code":"","descn":"","id":0,"cname":"name1"},{"code":"",
	 * "descn":"","id":0,"cname":"name2"}]
	 * 
	 * @param array
	 *            对象数组
	 * @param excludesProperties
	 *            不需要转换到json数据中的属性名称构成的数组 如String[]
	 *            {"roleses","departments","department"}
	 */
	public void outJsonArray(Object array, String[] excludesProperties,HttpServletResponse response) {
		if (excludesProperties != null)
			config.setExcludes(excludesProperties);
		outJsonString(JSONArray.fromObject(array, config).toString(),response);
	}
	
	/**
	 * 输出时间对象数组的json数据,如[{"code":"","descn":"","id":0,"cname":"name1"},{"code":"",
	 * "descn":"","id":0,"cname":"name2"}]
	 * 
	 * @param array
	 *            对象数组
	 * @param excludesProperties
	 *            不需要转换到json数据中的属性名称构成的数组 如String[]
	 *            {"roleses","departments","department"}
	 */
	public void outJsonArrayDate(Object array,HttpServletResponse response) {
//		if (excludesProperties != null) config.setExcludes(excludesProperties);
//		String[] dateFormats = new String[] {"yyyy/MM/dd","yyyy-MM-dd"};   
//		JSONUtils.getMorpherRegistry().registerMorpher(new DateMorpher(dateFormats));
		config.registerJsonValueProcessor(Date.class, new DateJsonValueProcessor());
		outJsonString(JSONArray.fromObject(array, config).toString(),response);
	}
	
	/**
	 * 可以设置不需转换成json数据格式的属性
	 * 
	 * @param obj
	 *            单个对象或者多个对象的集合
	 * @param excludesProperties
	 *            不需要转换到json数据中的属性名称构成的数组 如String[]
	 *            {"roleses","departments","department"}
	 */
	public void outJsonWithoutProperty(Object obj,HttpServletResponse response) {
		config.registerJsonValueProcessor(Date.class, new DateJsonValueProcessor());
		outJsonString(JSONObject.fromObject(obj, config).toString(),response);
	}

	public void outString(String str,HttpServletResponse response) {
		// getResponse().setHeader("Cache-Control", "no-cache");
		try {
			PrintWriter out = response.getWriter();
			out.write(str);
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
			log.error(e);
		}
	}

	public void outJsonString(String str,HttpServletResponse response) {
		response.setContentType("text/javascript;charset=UTF-8");
		outString(str,response);
	}

	private void outJsonHtmlString(String str,HttpServletResponse response) {
		response.setContentType("text/html;charset=UTF-8");
		outString(str,response);
	}

	public void outXMLString(String xmlStr,HttpServletResponse response) {
		response.setContentType("application/xml;charset=UTF-8");
		outString(xmlStr,response);
	}
}
